#include "Bullet.h"
#include "GameCore\Screen.h"
#include "GameCore\tinyxml2\tinyxml2.h"

BulletInitialValue Bullet::bulletInitialValueList[Bullet::BulletCount];

 Bullet::Bullet()
 {
	 if(bulletInitialValueList[GetBulletType()].spriteCache != NULL)
	 {
		 this->sprite = bulletInitialValueList[GetBulletType()].spriteCache;
	 }
	 else
	 {
		 bulletInitialValueList[GetBulletType()].spriteCache = SpriteManager::GetInstance()->LoadSpriteFromXMLFile(bulletInitialValueList[GetBulletType()].spriteName);
		 this->sprite = bulletInitialValueList[GetBulletType()].spriteCache;
	 }
	 
	 SetAnim(sprite->GetAnim(bulletInitialValueList[GetBulletType()].animID));
	 useAnim = true;
	 move = Vector2f(0.0, Screen::GetInstance()->GetHeight() / 2);
	 coolDownTime = 0.3f;
	 damage = 5;
 }
  Bullet::~Bullet()
  {
	  SafeDelete(animInst);
  }
  /*
 void Bullet::Render()
{
	DXDevice::GetInstance()->SetBlend(TRUE);
	sprite->DrawAnim(anim, position.x,position.y);
}
*/
  /*
void Bullet::Update(float timeDelta)
{
	GameObject::Update(timeDelta);
	if(position.y > Screen::GetInstance()->GetHeight() / 1.9f)
	{
		removed = true;
	}
}
*/
Bullet* Bullet::CreateBullet(Vector2f p)
{
	Bullet* b = new Bullet();
	b->position = p;
	return b;
}
void Bullet::Hit()
{
	hp = 0;
	if(hp <= 0) Destroy();
}
void Bullet::LoadBulletInitialValue(const char* data)
{
	int len = strlen(data);
	tinyxml2::XMLDocument doc;
	doc.Parse( data );
	if(doc.ErrorID() != 0)
	{
		return;
	}
	tinyxml2::XMLElement* root = doc.FirstChildElement("root");
	tinyxml2::XMLElement* bullet = root->FirstChildElement("bullet");
	while(bullet)
	{
		const char* bulletName = bullet->Attribute("name");
		const char* bulletSprite = bullet->Attribute("sprite");
		int hp = atoi(bullet->Attribute("hp"));
		int damage = atoi(bullet->Attribute("damage"));
		int animID = atoi(bullet->Attribute("animID"));
		BulletInitialValue goiv(bulletName, bulletSprite, hp, damage, animID);
		if(strcmp("BasicBullet", bulletName) == 0)
		{
			bulletInitialValueList[Bullet::BasicBullet] = goiv;
		}
		else if(strcmp("MachineGunBullet", bulletName) == 0)
		{
			bulletInitialValueList[Bullet::MachineGunBullet] = goiv;
		}
		bullet = bullet->NextSiblingElement("bullet");
	}
}